package com.tbit.uqbike.webmanager.controller;

import com.github.pagehelper.PageInfo;
import com.tbit.uqbike.object.pojo.UserInvitation;
import com.tbit.uqbike.object.pojo.statistics.ExportUserInvitation;
import com.tbit.uqbike.object.value.Page;
import com.tbit.uqbike.object.view.JsonResult;
import com.tbit.uqbike.service.business.UserInvitationService;
import com.tbit.uqbike.webmanager.util.Assert;
import com.tbit.uqbike.webmanager.util.PageHelperUtil;
import com.tbit.uqbike.webmanager.util.ServletUtils;
import com.tbit.uqbike.webmanager.util.excel.ExportExcelUtil;
import com.tbit.uqbike.webmanager.util.excel.ExportExcelWrapper;
import com.tbit.utils.DateTimeUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.support.RequestContext;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 用户邀请记录(UserInvitation)表控制层
 *
 * @author jing
 * @since 2020-12-31 14:33:21
 */
@RestController
@RequestMapping("/userinvitation")
public class UserInvitationController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private UserInvitationService userInvitationService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @PostMapping("/selectList")
    public JsonResult selectList(Integer id) {
        return JsonResult.ok(this.userInvitationService.queryById(id));
    }

    @PostMapping("/selectByAccountId")
    public JsonResult selectByAccountId(UserInvitation userInvitation) {
        Assert.notNull(userInvitation.getAccountId(), "ver.areaId.notNull");
        userInvitation.setPage(new Page(userInvitation.getPageNO(), userInvitation.getRowCount()));
        PageHelperUtil.initPage(userInvitation.getPage());
        PageInfo<UserInvitation> pageInfo = new PageInfo<>(userInvitationService.queryByUserInvitation(userInvitation));
        return JsonResult.ok(pageInfo.getList(), (int) pageInfo.getTotal());
    }

    @GetMapping("/exportByAccountId")
    public void exportByAccountId(UserInvitation userInvitation, HttpServletResponse response) {
        Assert.notNull(userInvitation.getAccountId(), "ver.areaId.notNull");
        List<ExportUserInvitation> exports = userInvitationService.exportByAccountId(userInvitation);
        ExportExcelWrapper<ExportUserInvitation> util = new ExportExcelWrapper<>();
        RequestContext context = getLogin(ServletUtils.getRequest()).getContext();
        String filename = context.getMessage("activity_invitation") + DateTimeUtil.getNowTimeYMD();
        String[] columnNames = {context.getMessage("activity_name"), context.getMessage("excel_head_name"), context.getMessage("excel_head_ridelog_phone"), context.getMessage("invitation_code"), context.getMessage("accepted")};
        util.exportExcel(filename, filename, columnNames, exports, response, ExportExcelUtil.EXCEL_FILE_2003);
    }

}